package com.hancheng.commoncloud.framework.config;

import cn.hutool.core.util.StrUtil;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

@Component
public class CustomLocaleResolver extends AcceptHeaderLocaleResolver {

    @Override
    public Locale resolveLocale(HttpServletRequest request) {
        String i18nHeader = request.getHeader("i18n");
        Locale locale = Locale.SIMPLIFIED_CHINESE;
        if (StrUtil.isNotBlank(i18nHeader)) {
            String[] parts = i18nHeader.split("_");
            if (parts.length == 2) {
                locale = new Locale(parts[0], parts[1]);
            } else {
                locale = new Locale(i18nHeader);
            }
        }
        LocaleContextHolder.setLocale(locale);
        // 默认使用中文
        return locale;
    }
}
